$(function() {
    let userid = getCookie("userid");
    let token = getCookie("token");

    function init() {
        var options = {
            url: "http://127.0.0.1:8888/cart/list",
            type: "GET",
            data: {
                id: userid,
            },
            headers: {
                authorization: token,
            },
            dataType: "json"
        };
        $.ajax(options)
            .done(function(res) {
                console.log(res);

                render(res);
            })
    }

    function render(res) {
        console.log(res.cart);
        $(".cart-container").html(
            res.cart.map(item =>
                `<div class="goods">
                          <div class="p-goods">
                                <div class="p-img">
                                      <img src="${item.img_small_logo}" alt="">
                                </div>
                                <div class="p-item">
                                      ${item.title}
                                </div>
                          </div>
                          <div class="p-price">
                                ￥${item.price}
                          </div>
                          <div class="p-quantity">
                                <div class="cartnum">
                                      <button>-</button>
                                      <input type="text" value=${item.cart_number}>
                                      <button>+</button>
                                </div>
                          </div>
                          <div class="p-sum"> ￥${ item.cart_number * item.price}</div>
                          <div class="p-options">
                                <a data-id=${ item.goods_id} href="javascript:void(0)">删除</a>
                          </div>
                    </div>`
            ).join("")
        )

    }
    init();


    $(".cate-all").mouseover(function() {
        $(".list").show();
    })
    $(".cate-all").mouseout(function() {
        $(".list").hide();
    })
})